package com.shuangk.majingze.coolpapa.utils;

import android.text.TextUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class SignUtils {

    public static final String KEY_SIGN = "sign";

    public static final String KEY_PRIVATE = "key";

    public static final String KEY_TIMESTAMP = "timestamp";

    public static String createEncryptionParam(Map<String, Object> param) {
        Collection<String> keySet = param.keySet();
        List<String> list = new ArrayList<String>(keySet);

        //对key键值按字典升序排序
        Collections.sort(list);
        String paramStr = "";
        LogUtils.i("提交的参数：--->");
        for (int i = 0; i < list.size(); i++) {
            paramStr += list.get(i) + "=" + param.get(list.get(i)) + "&";
            LogUtils.i(list.get(i) + " = " + param.get(list.get(i)));
        }
        if (!TextUtils.isEmpty(paramStr))
            paramStr = paramStr.substring(0, paramStr.length() - 1);
        String sign = "&" + KEY_SIGN + "=" + MD5Utils.MD5(paramStr);
        LogUtils.i("sign = " + MD5Utils.MD5(paramStr));

        if (paramStr.contains("&" + KEY_PRIVATE)) {
            paramStr = paramStr.replace("&" + KEY_PRIVATE + "=alsfoxShop_plat", "");
        } else {
            paramStr = paramStr.replace(KEY_PRIVATE + "=alsfoxShop_plat", "");
        }
        return "?" + paramStr + sign;
    }

    public static Map<String, Object> getParameters() {
        Map<String, Object> params = new HashMap<>();
        params.put(KEY_PRIVATE, "alsfoxShop_plat");
        params.put(KEY_TIMESTAMP, System.currentTimeMillis());
        return params;
    }
}